<?php


namespace app\common\cachemodel\zl2;


use app\common\model\BaseCache;
use think\Log;
use XznsTools\cache\CacheManage;

class Zl2LuckyPartnerRids extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl2_lucky_partner_rids';

    protected $cacheModel = 'fortune.lucky_partner';


    /**
     * 更新幸运结缘rids
     * @param int $uid
     * @param string $rids
     * @return bool
     */
    public function UpdateData(int $uid, string $rids)
    {
        try {
            $oldRids = $this->getLuckyPartnerRids($uid);

            if ($oldRids) {
                $this->where('id', (int)$oldRids['id'])->update(['rids' => $rids, 'update_time' => time()]);
            } else {
                $this->insert([
                    'uid' => $uid,
                    'rids' => $rids,
                    'create_time' => time()
                ]);
            }

            CacheManage::getInstance()->setCacheConfig($this->cacheModel)->setCacheKeyDetail('lucky_partner_rid', $uid)->rmCache('lucky_partner_rid');

            return true;
        } catch (\Exception $e) {
            Log::write('Zl2LuckyPartnerRids model => UpdateData()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');
            return false;
        }
    }


    /**
     * 获取 用户幸运结缘 的档案列表ID
     * @param int $uid
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getLuckyPartnerRids(int $uid)
    {
        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('lucky_partner_rid', $uid)
            ->getDataAndSet(function () use ($uid) {
                try {
                    return current($this->where(['uid' => $uid])->column('rids,id', 'uid'));
                } catch (\Exception $e) {
                    Log::write('Zl2LuckyPartnerRids model => getLuckyPartnerRids()异常：' . $e->getMessage() . '失败行数：' . $e->getLine(), 'error');

                    return '';
                }
            }, [], 'lucky_partner_rid');

        return $resData;
    }

    /**
     * 获取 用户幸运结缘 结果
     * @param int $uid
     * @param string $hash
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getLuckyPartner(int $uid, string $hash)
    {
        $resData = CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('lucky_partner', $uid . ':' . $hash)
            ->setCacheType('string')
            ->getCache([], 'lucky_partner');

        return $resData ? json_decode($resData, true) : false;
    }

    /**
     * 设置 用户幸运结缘 结果
     * @param int $uid
     * @param string $hash
     * @param array $data
     * @throws \Exception
     */
    public function setLuckyPartner(int $uid, string $hash, array $data)
    {
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);

        CacheManage::getInstance()->setCacheConfig($this->cacheModel)
            ->setCacheKeyDetail('lucky_partner', $uid . ':' . $hash)
            ->setCacheType('string')
            ->setCache($data, 'lucky_partner', false);
    }
}